import React, { useContext, useEffect, useState } from "react";
import { View, Text, StyleSheet, TouchableOpacity, Alert } from "react-native";
import { FlatList } from "react-native-gesture-handler";
import NoData from "../../components/preAdmin/NoData";
import ResearchCard from "../../components/preAdmin/ResearchCard";
import ResearchContext from "../../context/preparation/ResearchContext";
import LoadingBar from "../../components/InterView/LoadingBar";
const ResearchHome = ({ navigation }) => {
  const { researchLists, deleteResearch, isResearchLoading } =
    useContext(ResearchContext);
  const [deletedId, setDeletedId] = useState(null);

  const onConfirmeDeleted = (research) => {
    setDeletedId(research._id);
    Alert.alert("提示", "你确定要删除该调研报告吗?", [
      {
        text: "确定",
        onPress: () => {
          deleteResearch(research);
        },
      },
      {
        text: "取消",
      },
    ]);
  };

  return (
    <View style={styles.container}>
      <LoadingBar visible={isResearchLoading} />
      <FlatList
        style={{ padding: 16, paddingLeft: 10 }}
        data={researchLists}
        ListEmptyComponent={<NoData />}
        keyExtractor={(item, index) => {
          return index.toString();
        }}
        renderItem={({ item, index }) => (
          <ResearchCard
            item={item}
            isDelete={true}
            onDeleted={onConfirmeDeleted}
            deletedId={deletedId}
          />
        )}
      />
      <TouchableOpacity
        style={styles.Button}
        onPress={() => navigation.navigate("ResearchUpload")}
      >
        <Text style={styles.Text}>文件上传</Text>
      </TouchableOpacity>
    </View>
  );
};

var styles = StyleSheet.create({
  container: {
    backgroundColor: "#f5f5f5",
    flex: 1,
    justifyContent: "center",
    // padding: 16,
  },
  hint: {
    textAlign: "center",
  },
  Button: {
    borderRadius: 5,
    margin: 10,
    padding: 10,
    backgroundColor: "#3ABEF0",
    alignItems: "center",
    justifyContent: "center",
    elevation: 2, //设置阴影角度，通过这个设置有无阴影
    shadowColor: "gray", // 设置阴影色
  },
  Text: {
    fontSize: 16,
    color: "#fff",
    fontWeight: "bold",
  },
});

export default ResearchHome;
